package com.runshu.newsmartark.reader.helper;

import android.content.Context;
import android.os.AsyncTask;

import java.io.BufferedWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SaveLogFileThread extends AsyncTask<String, Integer, String> {
    String operation = null;
    BufferedWriter logWriter = null;
    Context context = null;
    InventoryBuffer inventoryBuffer = null;
    int logCount = 0;
    public SaveLogFileThread(Context context, String operation, BufferedWriter logWriter) {
        this.context = context;
        this.operation = operation;
        this.logWriter = logWriter;
    }

    public SaveLogFileThread(InventoryBuffer inventoryBuffer, String operation, BufferedWriter logWriter, int logCount) {
        this.operation = operation;
        this.inventoryBuffer = inventoryBuffer;
        this.logWriter = logWriter;
        this.logCount = logCount;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {
        if(operation.startsWith("save"))
        {
            String invType = operation.split("-")[1];
            if(invType.equalsIgnoreCase("REAL_TIME_INVENTORY"))
            {
                writeLogFileREAL(inventoryBuffer);
            } else if(invType.equalsIgnoreCase("FAST_SWITCH_ANT_INVENTORY"))
            {
                writeLogFileFAST(inventoryBuffer);
            }

//            Log.d(TAG, "doInBackground: nDataCount=" + inventoryBuffer.nDataCount);
//            Log.d(TAG, "doInBackground: nReadRate=" + inventoryBuffer.nReadRate);
        }
        else if(operation.equalsIgnoreCase("create"))
        {

        }
        return null;
    }

    public void writeLogFileREAL(InventoryBuffer log)
    {
        if(logWriter!=null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            try {
                logWriter.append(new StringBuilder()
                        .append(" -> Real [").append(sdf.format(new Date())).append("]")
                        .append("第").append(logCount).append("次")
                        .append(", 标签总数:").append(log.lsTagList.size()).append("个")
                        .append(", 标签应答的总记录数=").append(log.nDataCount).append("次")
                        .append(", 此轮命令标签识别速率=").append(log.nReadRate)
                        .toString());
                logWriter.newLine();
                logWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void writeLogFileFAST(InventoryBuffer log)
    {
        if(logWriter!=null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            try {
                logWriter.append(new StringBuilder()
                        .append(" -> Fast [").append(sdf.format(new Date())).append("]")
                        .append("第").append(logCount).append("次")
                        .append(", 标签总数:").append(log.lsTagList.size()).append("个")
                        .append(", 总共上传的标签数据记录数=").append(log.nDataCount)
                        .append(", 此轮命令标签识别速率=").append(log.nReadRate)
                        .append(", 命令总共消耗的时间(ms)=").append(log.nCommandDuration)
                        .toString());
                logWriter.newLine();
                logWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
